package it.clariter.dao;

import it.clariter.connection.MyConnection;
import it.clariter.model.Area;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class AreaDAO {

	public int insert(Area area)
	{
		Connection c=null;
		PreparedStatement ps=null;
		int result=0;
		try
		{
			c=MyConnection.getConnection();
			ps=c.prepareStatement("INSERT INTO "+DaoConstants.NOME_TABELLA_AREA+" SET "+DaoConstants.COLONNA_CODICE_AREA+"=?,"+DaoConstants.COLONNA_DESCRIZIONE_AREA+"=?");
			ps.setString(1, area.getCodice());
			ps.setString(2, area.getDescrizione());
			result=ps.executeUpdate();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				ps.close();
				c.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		return result;
	}
	public int update(Area area)
	{
		Connection c=null;
		PreparedStatement ps=null;
		int result=0;
		try
		{
			c=MyConnection.getConnection();
			ps=c.prepareStatement("UPDATE "+DaoConstants.NOME_TABELLA_AREA+" SET "+DaoConstants.COLONNA_CODICE_AREA+"=?,"+DaoConstants.COLONNA_DESCRIZIONE_AREA+"=? WHERE "+DaoConstants.COLONNA_ID_AREA+"=?");
			ps.setString(1, area.getCodice());
			ps.setString(2, area.getDescrizione());
			ps.setInt(3, area.getIdArea());
			result=ps.executeUpdate();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				ps.close();
				c.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		return result;
	}
	public int delete(int idva)
	{
		Connection c=null;
		PreparedStatement ps=null;
		int result=0;
		try
		{
			c=MyConnection.getConnection();
			ps=c.prepareStatement("DELETE FROM "+DaoConstants.NOME_TABELLA_AREA+" WHERE "+DaoConstants.COLONNA_ID_AREA+"=?");
			ps.setInt(1, idva);
			result=ps.executeUpdate();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				ps.close();
				c.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		return result;
	}
	public List<Area>getList()
	{
		Connection c=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		List <Area>list=new ArrayList<Area>();
		Area area=null;
		try
		{
			c=MyConnection.getConnection();
			ps=c.prepareStatement("SELECT * FROM "+DaoConstants.NOME_TABELLA_AREA);
			rs=ps.executeQuery();
			while(rs.next())
			{
				area=new Area();
				area=makeObject(area,rs);
				list.add(area);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				rs.close();
				ps.close();
				c.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		return list;
	}
	public Area get(int idva)
	{
		Connection c=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		Area area=null;
		try
		{
			c=MyConnection.getConnection();
			ps=c.prepareStatement("SELECT * FROM "+DaoConstants.NOME_TABELLA_AREA+" WHERE "+DaoConstants.COLONNA_ID_AREA+"=?");
			ps.setInt(1, idva);
			rs=ps.executeQuery();
			if(rs.next())
			{
				area=new Area();
				area=makeObject(area,rs);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				rs.close();
				ps.close();
				c.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		return area;
	}
	public List<Area> findByExample(Area area)
	{
		Connection c=null;
		Statement stm=null;
		ResultSet rs=null;
		Area area1=null;
		List<Area>list=new ArrayList<Area>();
		String query="SELECT * FROM "+DaoConstants.NOME_TABELLA_AREA+" WHERE 1=1 ";
		try
		{
			c=MyConnection.getConnection();
			if((area.getCodice()!=null)&&(!(area.getCodice().equals(""))))
			{
				query=query+" AND "+DaoConstants.COLONNA_CODICE_AREA+" LIKE '"+area.getCodice()+"%' ";
			}
			if((area.getDescrizione()!=null)&&(!(area.getDescrizione().equals(""))))
			{
				query=query+" AND "+DaoConstants.COLONNA_DESCRIZIONE_AREA+" LIKE '"+area.getDescrizione()+"%' ";
			}
			stm=c.createStatement();
			rs=stm.executeQuery(query);
			while(rs.next())
			{
				area1=new Area();
				area1=makeObject(area1,rs);
				list.add(area1);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				rs.close();
				stm.close();
				c.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		return list;
	}
	{

	}
	public Area makeObject(Area area,ResultSet rs) throws SQLException
	{
		area.setIdArea(rs.getInt(DaoConstants.COLONNA_ID_AREA));
		area.setCodice(rs.getString(DaoConstants.COLONNA_CODICE_AREA));
		area.setDescrizione(rs.getString(DaoConstants.COLONNA_DESCRIZIONE_AREA));
		return area;
	}
}

